package com.yiyi.credit.manage.sso.manager.controller;

import com.yiyi.credit.manage.sso.common.Constant;
import com.yiyi.credit.manage.sso.common.CookieConstants;
import com.yiyi.credit.manage.sso.common.exception.ResponseCode;
import com.yiyi.credit.manage.sso.common.exception.ServiceException;
import com.yiyi.credit.manage.sso.common.utils.CookieUtils;
import com.yiyi.credit.manage.sso.manager.common.handler.UserManagerClient;
import com.yiyi.credit.manage.sso.model.dto.CookieInfoDto;
import com.yiyi.credit.manage.sso.model.dto.response.Response;
import com.yiyi.credit.manage.sso.model.dto.response.SpApiResponse;
import com.yiyi.credit.manage.sso.model.entity.SysUser;
import com.yiyi.credit.manage.sso.service.inner.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
public class LoginController {

    //private static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    UserManagerClient userManagerClient;

    @Autowired
    SysUserService sysUserService;


    @RequestMapping(value = "manager/loginSysUser")
    public Object loginSysUser(@RequestParam String userName, @RequestParam String password,
                               HttpServletResponse response) throws Exception {
        if (StringUtils.isBlank(userName)) {
            throw new ServiceException(ResponseCode.USERNAME_NOTNULL);
        }
        if (StringUtils.isBlank(password)) {
            throw new ServiceException(ResponseCode.PWD_NOTNULL);
        }

        SysUser sysuser=sysUserService.getUserByName(userName);
        if(sysuser==null){
            throw new ServiceException(ResponseCode.NO_SYS_USER);
        }else{
            if(sysuser.getIsSysUser()!=1){
                throw new ServiceException(ResponseCode.NO_SYSTEM_USER);
            }
        }


        SpApiResponse<CookieInfoDto> spApiResponse = userManagerClient.loginSysUser(userName, password);
        if (null == spApiResponse) {
            throw new ServiceException(ResponseCode.SERVICE_ERR);
        }
        if (!Constant.SUCCESS_CODE.equals(spApiResponse.getCode())) {
            return new Response<>(spApiResponse.getCode(), spApiResponse.getMessage());
        }

        //返回信息
        CookieInfoDto cookieInfoDto = spApiResponse.getResult();

        //添加Cookie
        Cookie cookie = new Cookie(cookieInfoDto.getName(), cookieInfoDto.getToken());
        // 为true时用于https
        cookie.setSecure(cookieInfoDto.isSecure());
        cookie.setMaxAge(cookieInfoDto.getMaxAge());
        //cookie.setDomain(cookieInfoDto.getDomain());
        cookie.setPath(cookieInfoDto.getPath());
        response.addCookie(cookie);
        return new Response<>();
    }

    @RequestMapping(value = "manager/loginOut")
    public Object loginOut(HttpServletRequest request) throws Exception {
        // cookie获取token值
        String tokenValue = CookieUtils.getCookieByName(request.getCookies(), CookieConstants.NAME);
        SpApiResponse spApiResponse = userManagerClient.logout(tokenValue);
        if (null == spApiResponse) {
            throw new ServiceException(ResponseCode.SERVICE_ERR);
        }
        if (!Constant.SUCCESS_CODE.equals(spApiResponse.getCode())) {
            return new Response<>(spApiResponse.getCode(), spApiResponse.getMessage());
        }
        return new Response<>();
    }

}
